﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using DAO;
using BUS;
using Microsoft.Phone.Shell;
using System.Windows.Threading;
using System.Windows.Media.Imaging;

namespace ProjectM2Work.Pages
{
    public partial class PageTuDien : PhoneApplicationPage
    {
        static double SCAREWIDTH = 0f;
        static double SCAREHEIGHT = 0f;
        LongTextblock tbChiTiet = new LongTextblock();
        DispatcherTimer currentPosition = new DispatcherTimer();
        public PageTuDien()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

            SCAREWIDTH = System.Windows.Application.Current.Host.Content.ActualWidth / 480;
            SCAREHEIGHT = System.Windows.Application.Current.Host.Content.ActualHeight / 800;
            DefaultValueControl();

            //Media:
            meVideo.MediaOpened += new RoutedEventHandler(myMediaElement_MediaOpened);

            meVideo.MediaEnded += new RoutedEventHandler(myMediaElement_MediaEnded);

            meVideo.CurrentStateChanged += new RoutedEventHandler(myMediaElement_CurrentStateChanged);

            currentPosition.Tick += new EventHandler(currentPosition_Tick);

            //texeblock
            tbChiTiet.FontSize = 22;
            tbChiTiet.Foreground = new SolidColorBrush(Colors.Black);
            Panel.Children.Add(tbChiTiet.MainElement);
        }

        /// <summary>
        ///  Load data for the ViewModel Items
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
            App.ViewModel.LoadDataSelected();
        }

        /// <summary>
        /// set default value
        /// </summary>
        private void DefaultValueControl()
        {
            //set default value tbDienTu
            double Left = 72 * SCAREWIDTH;
            double Top = 2 * SCAREHEIGHT;
            tbDienTu.Margin = new Thickness(Left, Top, 0, 0);
            tbDienTu.Width = (333 - 83 + 2*13) * SCAREWIDTH;
            tbDienTu.Height = (73 - 15 + 2 * 13) * SCAREHEIGHT;

            //set default value lbTuDien
            Left = 83 * SCAREWIDTH;
            Top = 84 * SCAREHEIGHT;
            lbTuDien.Margin = new Thickness(Left, Top, 0, 0);
            lbTuDien.Width = (333 - 83) * SCAREWIDTH;
            lbTuDien.Height = (469 - 84) * SCAREHEIGHT;

            //set default value meVideo
            //Left = 300 * SCAREWIDTH;
            //Top = 88 * SCAREHEIGHT;
            //meVideo.Margin = new Thickness(Left, Top, 0, 0);
            //meVideo.Width = (760 - 373) * SCAREWIDTH;
            //meVideo.Height = (295 - 88) * SCAREHEIGHT;

            //set default value tbChiTiet
            //Left = 365 * SCAREWIDTH;
            //Top = 360 * SCAREHEIGHT;
            //tbChiTiet.Margin = new Thickness(Left, Top, 0, 0);
        }
        SolidColorBrush brushFirst = new SolidColorBrush(Colors.DarkGray);
        SolidColorBrush brushChange = new SolidColorBrush(Colors.Black);
        string strTimKiem = "Nhập Từ Vào Đây";
        private void tbDienTu_GotFocus(object sender, RoutedEventArgs e)
        {
            if (tbDienTu.Text == strTimKiem)
            {
                tbDienTu.Text = "";
                tbDienTu.Foreground = brushChange;
            }
            else
            {
                tbDienTu.Select(0, tbDienTu.Text.Length);
            }
        }

        private void tbDienTu_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (tbDienTu.Text != "" && tbDienTu.Text != strTimKiem)
            {
                App.ViewModel.LoadTuDienTimKiem(tbDienTu.Text);
            }
        }
        void myMediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
meVideo.Stop();
}

        void myMediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
performanceProgressBar.Maximum = (int)meVideo.NaturalDuration.TimeSpan.TotalMilliseconds;
meVideo.Play();
}
        private void tbDienTu_LostFocus(object sender, RoutedEventArgs e)
        {
            //if(tbDienTu.Text == strTimKiem)
            //tbDienTu.Text = strTimKiem;
            //tbDienTu.Foreground = brushFirst;
        }
        TuDien tuDienHienTai;
        private void lbTuDien_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (-1   != lbTuDien.SelectedIndex)
            {
                ItemTuVungModel panel = (ItemTuVungModel)((ListBox)sender).SelectedItem;

                tbTuVung.Text = panel.LineOne;
                //// find the selected ListBoxItem
                var selectedContainer = sender as FrameworkElement;
                TuVungBUS tuvungBUs = new TuVungBUS();
                tuDienHienTai = tuvungBUs.LayTuVungTheoID(panel.LineThree);
                if (tuDienHienTai != null)
                    tbChiTiet.Text = "Mô tả: \n" + tuDienHienTai.Description;
                
                btnSound();
            }
        }
        void currentPosition_Tick(object sender, EventArgs e)
{
performanceProgressBar.Value = (int)meVideo.Position.TotalMilliseconds;
}
        private void imgPlay_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            meVideo.Play();
            
        }

        private void imgPause_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            meVideo.Pause();
        }
        void myMediaElement_CurrentStateChanged(object sender, RoutedEventArgs e)
        {

            if (meVideo.CurrentState == MediaElementState.Playing)
            {

                currentPosition.Start();
                BitmapImage tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_play_disable.png", UriKind.Relative)).Stream);
                imgPlay.Source = tn;

                tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_pause_enable.png", UriKind.Relative)).Stream);
                imgPause.Source = tn;

                tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_stop_enable.png", UriKind.Relative)).Stream);
                imgStop.Source = tn;

            }

            else if (meVideo.CurrentState == MediaElementState.Paused)
            {

                currentPosition.Stop();
                BitmapImage tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_play_enable.png", UriKind.Relative)).Stream);
                imgPlay.Source = tn;

                tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_pause_disable.png", UriKind.Relative)).Stream);
                imgPause.Source = tn;

                tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_stop_enable.png", UriKind.Relative)).Stream);
                imgStop.Source = tn;


            }

            else
            {

                currentPosition.Stop();


                currentPosition.Stop();
                BitmapImage tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_play_enable.png", UriKind.Relative)).Stream);
                imgPlay.Source = tn;

                tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_pause_disable.png", UriKind.Relative)).Stream);
                imgPause.Source = tn;

                tn = new BitmapImage();
                tn.SetSource(Application.GetResourceStream(new Uri(@"/ProjectM2Work;component/images/btn_stop_disable.png", UriKind.Relative)).Stream);
                imgStop.Source = tn;

            }
        }
        private void imgStop_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            meVideo.Stop();
        }
        private void btnSound()
        {
            if (PublicAndStatic.HasInternetConection())
            {
                if (tuDienHienTai != null)
                {
                    meVideo.Source = PublicAndStatic.ConvertToUri(tuDienHienTai);
                    meVideo.Position = new TimeSpan(0, 0, 0, 0);
                    meVideo.Play();
                }
            }
            else
            {
                MessageBox.Show("Không có kết nối Internet.!");
            }
            //string duongdan = xulyMedia.layLinkSound(DsHienTai.tinhThanh);
            //if (duongdan != null)
            //{
            /*if (PublicAndStatic.HasInternetConection())
            {
                dMedia.StartDownloadFile("http://sieudidong.com.vn/DictVideo/" + tuDienHienTai.MediaPath,
                    tuDienHienTai.MediaPath, performanceProgressBar);
                dMedia._downloadFS += new DownloadMedia.DonwloadFinish(dMedia__downloadFS);
            }
            else
            {
                MessageBox.Show("Không có kết nối Internet..!!");
            }*/
            //}
            //else
            //{
            //    MessageBox.Show("Dữ liệu đang trong quá trình cập nhật");
            //}
        }

        private bool IsFull = false;
        private void imgFullScreen_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (!IsFull)
            {
                meVideo.Width = 300;
                meVideo.Height = 200;
                IsFull = true;
            }
            else
            {
                meVideo.Width = 280;
                meVideo.Height = 150;
                IsFull = false;
            }
        }

        private void image2_MouseLeave(object sender, MouseEventArgs e)
        {
            App.ZoomInImage(sender);
        }

        private void image2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            App.ZoomOutImage(sender);
        }

        private void image2_MouseMove(object sender, MouseEventArgs e)
        {
            App.ZoomOutImage(sender);
        }

        private void image2_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Pages/PageThongTin.xaml", UriKind.Relative));
        }

        private void image1_MouseLeave(object sender, MouseEventArgs e)
        {
            App.ZoomInImage(sender);
        }

        private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            App.ZoomOutImage(sender);
        }

        private void image1_MouseMove(object sender, MouseEventArgs e)
        {
            App.ZoomOutImage(sender);
        }

        private void image1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void image3_MouseLeave(object sender, MouseEventArgs e)
        {
            App.ZoomInImage(sender);
        }

        private void image3_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            App.ZoomOutImage(sender);
        }

        private void image3_MouseMove(object sender, MouseEventArgs e)
        {
            App.ZoomOutImage(sender);
        }

        private void image3_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //chuyen sang trang them tu
            NavigationService.Navigate(new Uri("/Pages/PageThemTu.xaml", UriKind.Relative));

        }

        private void imgShare_MouseLeave(object sender, MouseEventArgs e)
        {
            App.ZoomInImage(sender);
        }

        private void imgShare_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            App.ZoomOutImage(sender);
        }

        private void imgShare_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //Share từ lên FB
            if(tuDienHienTai!= null)
            {
                TuVungBUS tuVungBus = new TuVungBUS();
                tuVungBus.ShareTuVung(tuDienHienTai);
            }
        }

        private void imgShare_MouseMove(object sender, MouseEventArgs e)
        {
            App.ZoomOutImage(sender);
        }

        private void imgGopY_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //chuyen sang trang them tu
            //if (tuDienHienTai != null)
               // NavigationService.Navigate(new Uri("/Pages/PageThemTu.xaml?ID=" + tuDienHienTai.ID.ToString() + ",Tungu=" + tuDienHienTai.VnWord, UriKind.Relative));
        }

        private void imgGopY_MouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)
        {
            if (tuDienHienTai != null)
                NavigationService.Navigate(new Uri("/Pages/PageGopY.xaml?ID=" + tuDienHienTai.ID.ToString() + "&TuNgu=" + tuDienHienTai.VnWord, UriKind.Relative));
        }

        private void image3_MouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)
        {
            //chuyen sang trang them tu
            NavigationService.Navigate(new Uri("/Pages/PageThemTu.xaml", UriKind.Relative));
        }

    }
}